package com.jsmile.cloud.accountcenter.shop.feign;

import javax.validation.Valid;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jsmile.cloud.accountcenter.api.req.shop.ReqTenantCreate;
import com.jsmile.cloud.accountcenter.api.req.shop.ReqTenantDelete;
import com.jsmile.cloud.accountcenter.api.req.shop.ReqTenantQuery;
import com.jsmile.cloud.accountcenter.api.req.shop.ReqTenantUpdate;
import com.jsmile.cloud.accountcenter.api.res.ResTenant;
import com.jsmile.cloud.accountcenter.shop.service.TenantApplicationService;
import com.jsmile.mail.security.controller.AuthcController;
import com.jsmile.mail.security.handler.JsmileContext;
import com.jsmile.mall.api.JSmileResult;
import com.jsmile.mall.api.annotation.CurrentContext;
import com.jsmile.mall.api.constants.JSmileConstant;
import com.jsmile.mall.api.to.JsmilePage;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
@AllArgsConstructor
public class TenantServiceApiImpl extends AuthcController {

    private final TenantApplicationService tenantApplicationService;

    @RequestMapping(value = "/feign/tenant/getTenant", method = RequestMethod.POST)
    public JSmileResult<ResTenant> getTenant(@RequestBody @Valid ReqTenantDelete reqTenantDelete, @CurrentContext JsmileContext jsmileContext) {
        ResTenant tenant = tenantApplicationService.getTenant(reqTenantDelete.getMerchantId());
        return JSmileResult.success(tenant);
    }

    @RequestMapping(value = "/feign/tenant/addTenant", method = RequestMethod.POST)
    public JSmileResult<ResTenant> addTenant(@RequestBody @Valid ReqTenantCreate reqTenantSave, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.ACCOUNT_CENTER);
        ResTenant tenant = tenantApplicationService.addTenant(reqTenantSave);
        return JSmileResult.success(tenant);
    }

    @RequestMapping(value = "/feign/tenant/updateTenant", method = RequestMethod.POST)
    public JSmileResult<ResTenant> updateTenant(@RequestBody @Valid ReqTenantUpdate reqTenantUpdate, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.ACCOUNT_CENTER);
        ResTenant tenant = tenantApplicationService.updateTenant(reqTenantUpdate);
        return JSmileResult.success(tenant);
    }

    @RequestMapping(value = "/feign/tenant/getTenantPageList", method = RequestMethod.POST)
    public JSmileResult<JsmilePage<ResTenant>> getTenantPageList(@RequestBody @Valid ReqTenantQuery reqTenantQuery, @CurrentContext JsmileContext jsmileContext) {
        IPage<ResTenant> pages = tenantApplicationService.getTenantPageList(reqTenantQuery);
        return JSmileResult.success(new JsmilePage(reqTenantQuery.getPageIndex(), reqTenantQuery.getPageSize(), pages.getTotal(), pages.getRecords()));
    }

}
